package cn.zwz.customer.controller;

import cn.hutool.core.util.IdUtil;
import cn.zwz.basics.utils.PageUtil;
import cn.zwz.basics.utils.ResultUtil;
import cn.zwz.basics.baseVo.PageVo;
import cn.zwz.basics.baseVo.Result;
import cn.zwz.customer.entity.Supplier;
import cn.zwz.customer.service.ISupplierService;
import cn.zwz.data.utils.ZwzNullUtils;
import cn.zwz.customer.entity.Commodity;
import cn.zwz.customer.service.ICommodityService;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 熊英杰
 */
@Slf4j
@RestController
@Api(tags = "商品信息")
@RequestMapping("/zwz/commodity")
@Transactional
public class CommodityController {

    @Autowired
    private ICommodityService iCommodityService;

    @Autowired
    private ISupplierService iSupplierService;

    @RequestMapping(value = "/getOne", method = RequestMethod.GET)
    @ApiOperation(value = "查询单条商品信息")
    public Result<Commodity> get(@RequestParam String id){
        return new ResultUtil<Commodity>().setData(iCommodityService.getById(id));
    }

    @RequestMapping(value = "/count", method = RequestMethod.GET)
    @ApiOperation(value = "查询全部商品信息个数")
    public Result<Long> getCount(){
        return new ResultUtil<Long>().setData(iCommodityService.count());
    }

    @RequestMapping(value = "/getAll", method = RequestMethod.GET)
    @ApiOperation(value = "查询全部商品信息")
    public Result<List<Commodity>> getAll(){
        return new ResultUtil<List<Commodity>>().setData(iCommodityService.list());
    }

    /**
     * 查询商品信息
     * @param commodity 产品信息
     * @param page 分页参数
     * @return Result<IPage<Commodity>>
     */
    @RequestMapping(value = "/getByPage", method = RequestMethod.GET)
    @ApiOperation(value = "查询商品信息")
    public Result<IPage<Commodity>> getByPage(@ModelAttribute Commodity commodity ,@ModelAttribute PageVo page){
        QueryWrapper<Commodity> qw = new QueryWrapper<>();
        if(!ZwzNullUtils.isNull(commodity.getName())) {
            qw.like("name",commodity.getName());
        }
        if(!ZwzNullUtils.isNull(commodity.getSupplierName())) {
            qw.like("supplier_name",commodity.getSupplierName());
        }
        if(!ZwzNullUtils.isNull(commodity.getType())) {
            qw.like("type",commodity.getType());
        }
        IPage<Commodity> data = iCommodityService.page(PageUtil.initMpPage(page),qw);
        return new ResultUtil<IPage<Commodity>>().setData(data);
    }

    @RequestMapping(value = "/insertOrUpdate", method = RequestMethod.POST)
    @ApiOperation(value = "增改商品信息")
    public Result<Commodity> saveOrUpdate(Commodity commodity){
        if(ZwzNullUtils.isNull(commodity.getSupplierId())) {
            return ResultUtil.error("供应商不能为空");
        }
        Supplier supplier = iSupplierService.getById(commodity.getSupplierId());
        if(supplier == null) {
            return ResultUtil.error("供应商已被删除");
        }
        commodity.setSupplierName(supplier.getName());
        if(iCommodityService.saveOrUpdate(commodity)){
            return new ResultUtil<Commodity>().setData(commodity);
        }
        return ResultUtil.error();
    }

    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    @ApiOperation(value = "新增商品信息")
    public Result<Commodity> insert(Commodity commodity){
        if(ZwzNullUtils.isNull(commodity.getSupplierId())) {
            return ResultUtil.error("供应商不能为空");
        }
        Supplier supplier = iSupplierService.getById(commodity.getSupplierId());
        if(supplier == null) {
            return ResultUtil.error("供应商已被删除");
        }
        commodity.setSupplierName(supplier.getName());
        commodity.setCode(IdUtil.getSnowflake(1, 1).nextId() + "");
        iCommodityService.saveOrUpdate(commodity);
        return new ResultUtil<Commodity>().setData(commodity);
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ApiOperation(value = "编辑商品信息")
    public Result<Commodity> update(Commodity commodity) {
        if(ZwzNullUtils.isNull(commodity.getSupplierId())) {
            return ResultUtil.error("供应商不能为空");
        }
        Supplier supplier = iSupplierService.getById(commodity.getSupplierId());
        if(supplier == null) {
            return ResultUtil.error("供应商已被删除");
        }
        commodity.setSupplierName(supplier.getName());
        iCommodityService.saveOrUpdate(commodity);
        return new ResultUtil<Commodity>().setData(commodity);
    }

    @RequestMapping(value = "/delByIds", method = RequestMethod.POST)
    @ApiOperation(value = "删除商品信息")
    public Result<Object> delByIds(@RequestParam String[] ids){
        for(String id : ids){
            iCommodityService.removeById(id);
        }
        return ResultUtil.success();
    }
}
